export default function todosReducer(draft, action) {
  console.log(action)
  switch (action.type) {
    // 增加一条数据
    case 'add': {
      const {id, name, done} = action
      draft.unshift({id, name, done})
      break
    }
    // 选中单条数据
    case 'chooseTodo': {
      const {id, done} = action
      // 需要将新数组return来改变状态
      return draft.map(v => {
        if (v.id === id) return {...v, done}
        else return v
      })
    }
    // 删除单条数据
    case 'del': {
      const {id} = action
      return draft.filter(v => v.id !== id)
    }
    default: {
      throw Error(`未知的type${action.type}`)
    }
  }
}
